#include "SaveTheDam.h"

const bool SaveTheDam::_AILockChance[] = { 0,0,0,0,0,0,0,1,0,0 };

SaveTheDam::SaveTheDam():Scene(),_Bar1(0.0f),_Bar2(0.0f),_LockTime(0.0f)
{
	_TimeLeft = _TIMETOTURN_;
	_ReadyTime = _READYTIME_;
	_LockDelay = _LOCKDELAY_;
	_EndDelay = _ENDDELAY_;
	_Start = false;
}

void SaveTheDam::loadResource()
{
	LoadTGA(&_SkyTexture,"images/savethedam/sky.tga");
	_Sky = new Object2D(800,172);
	_Sky->setPosition(400,86);
	_Sky->faceRight();

	LoadTGA(&_StageTexture,"images/savethedam/overlay.tga");
	_GameStage = new Object2D(800,458);
	_GameStage->setPosition(400,370);

	LoadTGA(&_WaterTexture,"images/savethedam/water.tga");
	_Water = new Object2D(800,50);
	_Water->setPosition(400,160);

	_WheelSprite = new Sprite(2,1,"images/savethedam/wheel_turn_sprite.tga");
	_Wheel1 = new Object2D(108,108);
	_Wheel1->setPosition(200,520);
	_Wheel2 = new Object2D(108,108);
	_Wheel2->setPosition(600,520);

	_Wheel1Index = 0;
	_Wheel2Index = 0;

	LoadTGA(&_ChainTexture,"images/savethedam/chain.tga");
	LoadTGA(&_FishTexture,"images/savethedam/fish.tga");

	_Overlay = new Object2D(800,600);
	_Overlay->setPosition(400,300);
	LoadTGA(&_Ready,"images/savethedam/ready.tga");
	LoadTGA(&_Go,"images/savethedam/go.tga");
	LoadTGA(&_CrackTexture, "images/savethedam/crack.tga");

	_Crack1 = new Object2D(100,100);
	_Crack1->setPosition(100,200);
	_Crack2 = new Object2D(140,140);
	_Crack2->setPosition(600,220);
	_Crack3 = new Object2D(180,180);
	_Crack3->setPosition(350,300);


	_Fish  = new Object2D(80,80);
	_Fish->setPosition(100,180);
	_OverflowWater = new Object2D(50,240);
	_OverflowWater->setPosition(100,270);

	_Wheel1Lock = false;
	_Wheel2Lock = false;
	
	_TimeWord = new Object2D(60.0, 30.0, 0.0, false);
	_TimeWord->setPosition(380,10);
	LoadTGA(&_Time,"images/savethedam/Image_Time.tga");
	_TimeActual = new Object2D(45.0, 45.0, 0.0, false);
	_TimeActual->setPosition(380, 50);
	_Numbers = new Sprite(10,1,"images/savethedam/Image_Numbers.tga");
	LoadTGA(&_HowToPlay, "images/savethedam/howtoplay.tga");
	_TimeLeft = _TIMETOTURN_;
	_ReadyTime = _READYTIME_;
	_LockDelay = _LOCKDELAY_;
	_Start = false;

	LoadTGA(&_P1Win, "images/savethedam/Image_GameOverP1Win.tga");
	LoadTGA(&_P2Win, "images/savethedam/Image_GameOverP2Win.tga");
}

void SaveTheDam::freeResource()
{
	freeTexture(&_SkyTexture);
	delete _Sky;
	freeTexture(&_StageTexture);
	freeTexture(&_FishTexture);
	freeTexture(&_ChainTexture);
	//delete &_StageTexture;
	delete _GameStage;
	freeTexture(&_WaterTexture);
	//delete &_WaterTexture;
	delete _Water;
	delete _Wheel1;
	delete _Wheel2;

	delete _Overlay;
	freeTexture(&_Go);
	freeTexture(&_Ready);
	freeTexture(&_CrackTexture);

	delete _Crack1;
	delete _Crack2;
	delete _Crack3;

	delete _Fish;
	delete _TimeWord;
	freeTexture(&_Time);
	freeTexture(&_HowToPlay);
	freeTexture(&_P1Win);
	freeTexture(&_P2Win);
}

void SaveTheDam::reset()
{
	_TimeLeft = _TIMETOTURN_;
	_Bar1 = 0.0f;
	_Bar2 = 0.0f;
	_Wheel1Index = 0;
	_Wheel2Index = 0;
	_Wheel1Lock = false;
	_Wheel2Lock = false;
	_LockTime = 0.0f;
	_ReadyTime = _READYTIME_;
	_LockDelay = _LOCKDELAY_;
	_Start = false;
	_EndDelay = _ENDDELAY_;
	_Water->setPosition(400,160);
}

void SaveTheDam::draw()
{
	if(!_Start)
	{
		_Overlay->drawPlane(_HowToPlay.texID);
	}
	else
	{
		if(_ReadyTime > 0.0f)
		{
			if(_ReadyTime < 0.7f)
				_Overlay->drawPlane(_Go.texID);
			else
				_Overlay->drawPlane(_Ready.texID);
		}
		else
		{
			if(_TimeLeft > 0.0f)
			{
				_Sky->drawPlane(_SkyTexture.texID);
				_Water->drawPlane(_WaterTexture.texID);
				_GameStage->drawPlane(_StageTexture.texID);
				_TimeWord->drawPlane(_Time.texID);
				if(_TimeLeft < 4.0f)
					glColor3f(1,0,0);
				_TimeActual->drawPlane(_Numbers,(int)_TimeLeft);
				glColor3f(1,1,1);
				_Wheel1->drawPlane(_WheelSprite,_Wheel1Index);
				if(_TimeLeft < 8.0f)
					_Crack1->drawPlane(_CrackTexture.texID);
				if(_TimeLeft < 7.0f)
					_Crack2->drawPlane(_CrackTexture.texID);
				if(_TimeLeft < 4.0f)
					_Crack3->drawPlane(_CrackTexture.texID);
				if(_Wheel1Lock)
					_Wheel1->drawPlane(_ChainTexture.texID);	
				_Fish->drawPlane(_FishTexture.texID);
				glColor4f(1,1,1,0.4f);
				_OverflowWater->drawPlane(_WaterTexture.texID);
				glColor4f(1,1,1,1);
				if(!_SinglePlayer)
				{
					if(_Wheel2Lock)
						_Wheel2->drawPlane(_ChainTexture.texID);
					_Wheel2->drawPlane(_WheelSprite,_Wheel2Index);
				}
				
				
			}
			else
			{
				if(_SinglePlayer)
				{
					_Overlay->drawPlane(_P1Win.texID);
				}
				else
				{
					if(_Bar1 > _Bar2)
					{
						_Overlay->drawPlane(_P1Win.texID);
					}
					else
					{
						_Overlay->drawPlane(_P2Win.texID);
					}
				}
				//_Overlay->drawPlane(_HowToPlay.texID);
			}
		}
	}
}

void SaveTheDam::update()
{
	float dTime = 1 / RealTimeController::getInstance().getFPS();
	if(!_Start)
	{
	}
	else
	{
		if(_ReadyTime > 0.0f)
		{
			_ReadyTime -= dTime;
			
		}
		else
		{
			if(_TimeLeft > 0.0f)
			{
				if(_Fish->getPosition().getY() < 350.0f)
					_Fish->move(_GRAVITY2D_ / 2);
				else
				{
					_Fish->setPosition(Randomizer::getInstance().randMax(700,100),180);
					_OverflowWater->setPosition(_Fish->getPosition().getX(),270);
				}
				if(_Bar1 > 50.0f)
					_Bar1 -= _DECREMENTFORCE_ * dTime;
				else
					_Bar1 -= _DECREMENTFORCE_/2 * dTime;
				if(_Bar1 < 0.0f)
					_Bar1 = 0.0f;
				if(_Bar2 > 50.0f)
					_Bar2 -= _DECREMENTFORCE_ * dTime;
				else
					_Bar2 -= _DECREMENTFORCE_/2 * dTime;
				if(_Bar2 < 0.0f)
					_Bar2 = 0.0f;
				_TimeLeft -= dTime;
				if(_Water->getPosition().getY() > 130.0f)
				_Water->move(0,-0.1f);

				if(_LockTime == 0.0f && _LockDelay == 0.0f)
				{
					int key = Randomizer::getInstance().randMax(10);
					_Wheel1Lock = false;
					_Wheel2Lock = false;
					if(_AILockChance[key] == true)
					{
						if(_SinglePlayer == true)
						{
							_Wheel1Lock = true;
						}
						else
						{
							bool _LockP1 = Randomizer::getInstance().randMax(10) % 2;
							if(_LockP1)
							{
								_Wheel1Lock = true;
								Logger::getInstance().addLog("Locked Player 1");
							}
							else
							{
								_Wheel2Lock = true;
								Logger::getInstance().addLog("Locked Player 2");
							}				
						}
						_LockTime = _LOCKTIME_;
						_LockDelay = _LOCKDELAY_;
					}
				}
				else
				{
					if(_LockTime > 0.0f)
						_LockTime -= dTime;
					else
						_LockDelay -= dTime;
				}
			}
			else
			{
				if(_EndDelay > 0.0f)
				{
					_EndDelay -= dTime;
				}
				else
				{
					(_Bar1 > _Bar2) ? LeaderBoard::getInstance().P1_AddScore() : LeaderBoard::getInstance().P2_AddScore();
					SceneManager::getInstance().changeScene("Score_Board");
				}
			}
			if(_TimeLeft < 9.0f)
				PlaySound("sounds/savethedam/ice_cracking_large_01.wav",NULL,SND_FILENAME|SND_ASYNC);
			if(_TimeLeft < 0.0f)
				_TimeLeft = 0.0f;
			if(_LockTime < 0.0f)
				_LockTime = 0.0f;
			if(_LockDelay < 0.0f)
				_LockDelay = 0.0f;
		}
	}
}

void SaveTheDam::handleKeyboard(int key, int x, int y)
{
	switch ( key ) {
	//case 27 : glutLeaveMainLoop();
	//break;
	default : break;
	}
}
void SaveTheDam::handleKeyboardUp(int key, int x, int y)
{
	if(!_Start)
	{
		if(key == ' ')
		{ 
			_Start = true;
			PlaySound("sounds/games/go.wav", NULL,SND_ASYNC);
		}
	}
	if(_TimeLeft > 0.0f)
	{
		if((key == 'a' || key == 's' || key == 'd' || key == 'A' || key == 'S' || key == 'D') && !_Wheel1Lock)
		{
			_Bar1 += _TURNFORCE_;
			_Wheel1Index = !_Wheel1Index;
		}
		if((key == 'j' || key == 'l' || key == 'k' || key == 'J' || key == 'L' || key == 'K' ) && !_Wheel2Lock)
		{
			_Bar2 += _TURNFORCE_;
			_Wheel2Index = !_Wheel2Index;
		}
		if(_Bar1 >= _MAXFORCE_)
			_Bar1 = 99.0f;
		if(_Bar2 >= _MAXFORCE_)
			_Bar2 = 99.0f;
		_Water->move(0,0.5f);
		
	}
	else
	{
		SceneManager::getInstance().changeScene("Score_Board");
	}
}
void SaveTheDam::handleKeyboardSpecial(int key, int x, int y)
{
	switch(key)
	{
	//case GLUT_KEY_F12:
		//reset();
		break;
	}
}
void SaveTheDam::getMid(int &x, int &y)
{

}
void SaveTheDam::handleMouseClick(int button, int state, int x, int y)
{
	switch( button )
	{
	case GLUT_LEFT_BUTTON :
		break;
	case GLUT_RIGHT_BUTTON : 
		
		break;
	case GLUT_MIDDLE_BUTTON : break;
	}
}
void SaveTheDam::handleMouseMove(int x, int y)
{
}